home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13149 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.1 KB  |  119 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: Philippe Verdy <100105.3120@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: object creation from an abstract base class
  5. Date: 24 Mar 1996 01:16:48 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4j27q0$shm@arl-news-svc-3.compuserve.com>
  8. NNTP-Posting-Host: ad53-232.compuserve.com
  9.  
  10. Wang TianXing <gztxwang@public1.guangzhou.gd.cn> s'Θcrit :
  11. > On 23 Mar 1996 18:22:26 GMT, grantp@usa.pipeline.com(Pete Grant)
  12. > wrote:
  13. > | On Mar 21, 1996 08:59:50 in article <Re: object creation from an abstract
  14. > | base class>, 'Sukanta Ganguly <sukanta_ganguly@novell.com>' wrote: 
  15. > |  
  16. > |  
  17. > | >Michael Catello wrote: 
  18. > | >>  
  19. > | >> Hello OOPsters, 
  20. > | >>  
  21. > | >> I was just looking for validation/other suggestions for a method I 
  22. > | >> recently used in a program. I have defined an abstract base class 
  23. > | >> (i.e. contains pure virtual functions), all access to the derived 
  24. > | >> classes of this base are thru a pointer to the base class. To create 
  25. > | >> the actual objects of the derived classes I used the following scheme: 
  26. > | >>  
  27. > | >> enum FooType {BAR, BAS}; 
  28. > | >>  
  29. > | >> // base class 
  30. > | >> class CFoo 
  31. > | >>         { 
  32. > | >>         CFoo(); 
  33. > | >>         ~CFoo(); 
  34. > | >>  
  35. > | >>         static CFoo* CreateFoo(FooType type); 
  36. > | >>  
  37. > | >>         // other methods/data including pure virtual fns whose behaviour
  38. > |  
  39. > | >will 
  40. > | >> be defined in the derived classes 
  41. > | >>         }; 
  42. > | >>  
  43. > | >> class CBar: public CFoo 
  44. > | >>         { 
  45. > | >>         // 
  46. > | >>         }; 
  47. > | >>  
  48. > | >> class CBas: public CFoo 
  49. > | >>         { 
  50. > | >>         // 
  51. > | >>         }; 
  52. > | >>  
  53. > | >> CFoo* CFoo::CreateFoo(FooType type) 
  54. > | >>         { 
  55. > | >>         CFoo* pfoo = NULL; 
  56. > | >>  
  57. > | >>         switch (type) 
  58. > | >>                 { 
  59. > | >>                 case BAR: 
  60. > | >>                         pfoo = new CBar; 
  61. > | >>                         break; 
  62. > | >>                 case BAS: 
  63. > | >>                         pfoo = new CBas; 
  64. > | >>                         break; 
  65. > | >>                 } 
  66. > | >>  
  67. > | >>         return pfoo; 
  68. > | >>         } 
  69. > | >>  
  70. > | >> main() 
  71. > | >>         { 
  72. > | >>         CFoo* interface = CFoo::CreateFoo(BAR); 
  73. > | >>         } 
  74. > | >>  
  75. > | >> Obviously it is the CreateFoo() function that I am wondering about. In 
  76. > | >> the actual implementation I had multiple static "Create" functions for 
  77. > | >> the base class that would allow me to create a new object: one based 
  78. > | >> on an enumerated token (shown above), another an existing object, as 
  79. > | >> well as one based on the format of a datafile. My application never 
  80. > | >> references any of the derived classes directly, except in their 
  81. > | >> creation and definition. 
  82. > | >>  
  83. > | >> Is there another/better/more appropriate way to handle this type of 
  84. > | >> object creation? Thanks for your assistance, 
  85. > | >>  
  86. > | > [incorrect things deleted]
  87. > |  
  88. > | BTW, the statement "pfoo = new CBas;" is perfectly legal, 
  89. > | and even proper.  Any compiler that "cribs" about it is wrong. 
  90. > So, the original poster should write code like this:
  91. >     CFoo *interface = new CBar;
  92. > and remove the static function CFoo::CreateFoo(FooType);
  93. No ! We need such a function in many C++ programs which have
  94. to manage presistent collections of objects. However, such
  95. applications do not use an enumeration but a registration
  96. base class which is called by adding a static member to each
  97. derived class, which will register themselves and obtain a
  98. valid Id collected on a persistent stream. But on the final
  99. result, managing such collections require that we have a
  100. CFoo::CreateFoo(FooType) function in order to recreate the
  101. collection of objects from a source which only enumerates
  102. FooTypes. This is useful to create containers of graphic
  103. objects with common attributes, and varying parameters
  104. stored on a persistent stream, so that we can load and draw
  105. the collection of objects without knowing anything on them.
  106. Your code is legal Wang but you can generalize it to avoid
  107. the switch() and the enumeration.
  108. Philippe Verdy
  109. > ---
  110. > Wang TianXing
  111.  
  112.